import dayjs from "dayjs";
import { usePublicHooks } from "@/views/system/hooks";
import { message } from "@/utils/message";
import { addDialog } from "@/components/ReDialog";
import editForm from "../form.vue";
import { reactive, ref, onMounted, h, toRaw } from "vue";
import { type FormItemProps } from "./types";
import { type PaginationProps } from "@pureadmin/table";
import {
  delSysWeiXinMsgModes,
  getSysWeiXinMsgModeAllList,
  saveSysWeiXinMsgModes
} from "@/api/systemcofig";
export function weichatMessgaeConfig() {
  const dataList = ref([]);
  const loading = ref(false);
  const formRef = ref();
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 10,
    currentPage: 1,
    background: true
  });
  usePublicHooks();
  const columns: TableColumnList = [
    {
      label: "编号",
      prop: "id",
      minWidth: 100,
      sortable: "custom"
    },
    {
      label: "模板名称",
      prop: "tempName",
      minWidth: 230,
      sortable: "custom"
    },
    {
      label: "模板code",
      prop: "tempCode",
      minWidth: 150
    },
    {
      label: "机构代码",
      prop: "comID"
    },
    {
      label: "创建时间",
      minWidth: 180,
      prop: "addTime",
      formatter: ({ addTime }) => dayjs(addTime).format("YYYY-MM-DD HH:mm:ss")
    },
    {
      label: "操作",
      fixed: "right",
      width: 240,
      slot: "operation"
    }
  ];

  function handleSizeChange(val: number) {
    pagination.pageSize = val;
    onSearch();
  }

  function handleCurrentChange(val: number) {
    pagination.currentPage = val;
    onSearch();
  }

  // async function onSearch() {
  //   loading.value = true;
  //   const Result = await getSysWeiXinMsgModeAllList(toRaw(pagination));
  //   dataList.value = Result.result;
  //   pagination.total = Result.onePage.total;
  //   pagination.pageSize = Result.onePage.pageSize;
  //   pagination.currentPage = Result.onePage.currentPage;
  //   setTimeout(() => {
  //     loading.value = false;
  //   }, 500);
  // }

  async function onSearch() {
    loading.value = true;
    const Result = await getSysWeiXinMsgModeAllList(toRaw(pagination));
    dataList.value = Result.result;
    pagination.total = Result.pageInfo.total;
    pagination.pageSize = Result.pageInfo.pageSize;
    pagination.currentPage = Result.pageInfo.currentPage;

    setTimeout(() => {
      loading.value = false;
    }, 500);
  }

  async function handleDelete(row) {
    const Result = await delSysWeiXinMsgModes({ id: row.id });
    if (Result.isSuccess) {
      message(`已删除名称为 ${row.tempName} 的数据`, {
        type: "success"
      });
      onSearch();
    } else {
      message(Result.msg, { type: "error" });
    }
  }

  onMounted(() => {
    onSearch();
  });

  function openDialog(title = "新增", row?: FormItemProps) {
    addDialog({
      title: `${title}微信消息模板`,
      props: {
        formInline: {
          id: row?.id ?? 0,
          tempName: row?.tempName ?? "",
          tempCode: row?.tempCode ?? "",
          tempContext: row?.tempContext ?? "",
          tempHtml: row?.tempHtml ?? ""
        }
      },
      width: "40%",
      draggable: true,
      fullscreenIcon: true,
      closeOnClickModal: false,
      contentRenderer: () => h(editForm, { ref: formRef }),
      beforeSure: (done, { options }) => {
        const FormRef = formRef.value.getRef();
        const curData = options.props.formInline as FormItemProps;
        function chores() {
          message(`您${title}了名称为${curData.tempName}的这条数据`, {
            type: "success"
          });
          done();
          onSearch();
        }
        FormRef.validate(async valid => {
          if (valid) {
            console.log(curData);
            const subResult = await saveSysWeiXinMsgModes(curData);

            if (subResult.isSuccess) {
              chores();
            } else {
              message(subResult.msg, { type: "error" });
            }
          }
        });
      }
    });
  }

  const getConfigList = () => {
    console.log("this is a fnuntong");
  };

  return {
    getConfigList,
    columns,
    pagination,
    dataList,
    onSearch,
    loading,
    openDialog,
    handleDelete,
    handleSizeChange,
    handleCurrentChange
  };
}
